﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PyNet
{
    public partial class frmSettings : Form
    {
        private Size f_size;

        public frmSettings()
        {
            InitializeComponent();
            
        }

        private void LoadAllSettings()
        {
            txtAdmins.Text = clsFileSystem.ReadData("admins.txt");
            txtMOTD.Text = clsFileSystem.ReadData("motd.txt");
            txtCapacity.Text = Properties.Settings.Default.serverCapacity.ToString();
            txtPort.Text = Properties.Settings.Default.serverPort.ToString();
            chkSSD.Checked = Properties.Settings.Default.serverShowSD;
        }

        private void SaveAllSettings()
        {
            int port = 1337;
            try
            {
                port = int.Parse(txtPort.Text);
            }
            catch
            {
                txtPort.Text = "1337";
            }

            if (port < 100 || port > 21000)
            {
                txtPort.Text = "1337";
            }

            int val = 1337;
            try
            {
                val = int.Parse(txtCapacity.Text);
            }
            catch { txtCapacity.Text = "500"; }

            if (val < 2 || val > 99999)
            {
                txtCapacity.Text = "500";
            }

            clsFileSystem.WriteData("admins.txt", txtAdmins.Text);
            clsFileSystem.WriteData("motd.txt", txtMOTD.Text);
            Properties.Settings.Default.serverCapacity = int.Parse(txtCapacity.Text);
            Properties.Settings.Default.serverPort = int.Parse(txtPort.Text);
            Properties.Settings.Default.serverShowSD = chkSSD.Checked;
        }

        private void ResetAllSettings()
        {
            txtAdmins.Text = "";
            txtCapacity.Text = "500";
            txtMOTD.Text = "Welcome to PyNet!\r\nThis server is running PyNet2 by VBScript";
            txtPort.Text = "1337";
            chkSSD.Checked = true;
        }

        private void frmSettings_Load(object sender, EventArgs e)
        {
            LoadAllSettings();
            f_size = this.Size;
        }

        private void frmSettings_Resize(object sender, EventArgs e)
        {
            if (f_size.Width > 0 && f_size.Height > 0)
                this.Size = f_size;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            SaveAllSettings();
            this.Close();
        }

        private void btnDefault_Click(object sender, EventArgs e)
        {
            ResetAllSettings();
        }

        private void txtPort_TextChanged(object sender, EventArgs e)
        {
            if (f_size.Width > 0 && f_size.Height > 0)
                txtPortWarning.Text = "The server must be restarted for port to take effect.";
        }

        private void btnViewFolder_Click(object sender, EventArgs e)
        {
            FileInfo f = new FileInfo(Application.ExecutablePath);
            System.Diagnostics.Process.Start(f.DirectoryName);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clsFileSystem.CheckDir("Ver");
            FileInfo f = new FileInfo(Application.ExecutablePath);
            System.Diagnostics.Process.Start(f.DirectoryName + "\\Ver");
        }
    }
}
